<?php

class Journal2HtmlClasses {

    private $classes = array();

    public function __construct($registry) {
        $server = $registry->get('request')->server;
        if (!isset($server['HTTP_USER_AGENT'])) return;
        $ua = strtolower($server['HTTP_USER_AGENT']);

        $g = 'gecko';
        $w = 'webkit';
        $s = 'safari';
        $b = array();

        // browser
        if(!preg_match('/opera|webtv/i', $ua) && preg_match('/msie\s(\d)/', $ua, $array)) {
            $b[] = 'ie ie' . $array[1];
        }	else if(strstr($ua, 'firefox/2')) {
            $b[] = $g . ' ff2';
        }	else if(strstr($ua, 'firefox/3.5')) {
            $b[] = $g . ' ff3 ff3_5';
        }	else if(strstr($ua, 'firefox/3')) {
            $b[] = $g . ' ff3';
        } else if(strstr($ua, 'gecko/')) {
            $b[] = $g;
        } else if(preg_match('/opera(\s|\/)(\d+)/', $ua, $array)) {
            $b[] = 'opera opera' . $array[2];
        } else if(strstr($ua, 'konqueror')) {
            $b[] = 'konqueror';
        } else if(strstr($ua, 'chrome')) {
            $b[] = $w . ' ' . $s . ' chrome';
        } else if(strstr($ua, 'iron')) {
            $b[] = $w . ' ' . $s . ' iron';
        } else if(strstr($ua, 'applewebkit/')) {
            $b[] = (preg_match('/version\/(\d+)/i', $ua, $array)) ? $w . ' ' . $s . ' ' . $s . $array[1] : $w . ' ' . $s;
        } else if(strstr($ua, 'mozilla/')) {
            $b[] = $g;
        }

        // platform
        if(strstr($ua, 'j2me')) {
            $b[] = 'mobile';
        } else if(strstr($ua, 'iphone')) {
            $b[] = 'iphone';
        } else if(strstr($ua, 'ipod')) {
            $b[] = 'ipod';
        } else if(strstr($ua, 'mac')) {
            $b[] = 'mac';
        } else if(strstr($ua, 'darwin')) {
            $b[] = 'mac';
        } else if(strstr($ua, 'webtv')) {
            $b[] = 'webtv';
        } else if(strstr($ua, 'win')) {
            $b[] = 'win';
        } else if(strstr($ua, 'freebsd')) {
            $b[] = 'freebsd';
        } else if(strstr($ua, 'x11') || strstr($ua, 'linux')) {
            $b[] = 'linux';
        }

        foreach ($b as $c) {
            foreach (explode(' ', $c) as $cc) {
                $this->addClass($cc);
            }
        }
    }

    public function addClass($class) {
        $this->classes[$class] = $class;
    }

    public function hasClass($class) {
        return isset($this->classes[$class]);
    }

    public function removeClass($class) {
        if ($this->hasClass($class)) {
            unset($this->classes[$class]);
        }
    }

    public function getAll() {
        return implode(' ', $this->classes);
    }

}